package softtech.apps.autoanswerphonecall;

import java.io.File;
import java.util.ArrayList;





import android.app.ActionBar;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.os.Bundle;
import android.os.Environment;
import android.support.v4.app.NavUtils;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class ListVoiceMessageActivity extends Activity {
	private static final String AUDIO_RECORDER_FOLDER = "VoiceMessage";

	private File folder;
	private File files[];

	AudioManager audioManager;
	ArrayList<String> VoiceMessageReceived = new ArrayList<String>();
	ArrayList<String> VoiceMessageReceivedTitle = new ArrayList<String>();
	
	Context context = this;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_list_voice_message);

		ActionBar bar = getActionBar();
		bar.setDisplayHomeAsUpEnabled(true);
		bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#2ac2d4")));

		String filepath = Environment.getExternalStorageDirectory().getPath();
		folder = new File(filepath, AUDIO_RECORDER_FOLDER);
		if (!folder.exists()) {
			folder.mkdirs();
		}
		files = folder.listFiles();

		if (!files.equals(null)) {
			
			ListVoiceMessageAdapter MesVoiceAdapter = new ListVoiceMessageAdapter(context);
			
			ListView lvAdapter = (ListView) findViewById(R.id.lvMessageReceived);
			
			lvAdapter.setAdapter(MesVoiceAdapter);
			lvAdapter.setOnItemClickListener(new OnItemClickListener() {

				@Override
				public void onItemClick(AdapterView<?> arg0, View arg1,
						int arg2, long arg3) {
					// TODO Auto-generated method stub
					// VoiceMessageReceived[arg2]
					PlayMusic(VoiceMessageReceived.get(arg2));
					arg1.setBackgroundColor(Color.parseColor("#ffaa00"));
				}
			});

		} else {
			TextView tv = (TextView) findViewById(R.id.tvNotice);
			tv.setText("Have no voice message has received");
		}
	}

	private void PlayMusic(String filePath) {

		Log.d("MUSIC","Start to play music at path "+filePath);
		AudioManager am = (AudioManager) getBaseContext().getSystemService(
				Context.AUDIO_SERVICE);
		am.setMode(AudioManager.STREAM_MUSIC);
		// am.setSpeakerphoneOn(true);

		am.setStreamVolume(AudioManager.STREAM_MUSIC,
				am.getStreamMaxVolume(AudioManager.STREAM_MUSIC), 0);

		MediaPlayer mediaPlayer = new MediaPlayer();
		mediaPlayer.setOnPreparedListener(new OnPreparedListener() {
			@Override
			public void onPrepared(MediaPlayer mp) {
				// Do something. For example: playButton.setEnabled(true);
				//mp.start();
			}
		});
		mediaPlayer.setLooping(false);
		mediaPlayer.setVolume(AudioManager.MODE_IN_CALL, AudioManager.MODE_IN_CALL);

		try {
			mediaPlayer.setDataSource(filePath);
		} catch (Exception e) {
			e.printStackTrace();
		}
		mediaPlayer.start();

		mediaPlayer.setOnCompletionListener(new OnCompletionListener() {

			@Override
			public void onCompletion(MediaPlayer mp) {
				// TODO Auto-generated method stub
				mp.stop();
				mp.release();
				mp = null;
				Log.d("MUSIC","Stop play music at ListVoiceMessageActivity");
			}

		});

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.list_voice_message, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		switch (item.getItemId()) {
		case android.R.id.home:
			NavUtils.navigateUpFromSameTask(this);
			return true;
		default:
		}
		return super.onOptionsItemSelected(item);
	}

}
